﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.SceneManagement;

public class BigStepsManager : MonoBehaviour
{
    public GameObject m_RoundBottpmFlask;//圆底烧瓶
    public GameObject m_DropBottle;//瓶子
    public GameObject m_RubberDropper;//胶头滴管
    public GameObject m_RubberStopperDoubleKo;//组合管
    public GameObject m_Beaker;//烧杯
    public GameObject m_WaterStopClip;//夹子
    public GameObject m_RubberStopper;//塞子
    public GameObject m_IronPlatform;
    public GameObject m_BeakerBrush;
    public StepsManager m_StepsManager;

    RoundBottpmFlask roundBottpmFlask;//圆底烧瓶
    DropBottle dropBottle;//瓶子
    RubberDropper rubberDropper;//胶头滴管
    RubberStopperDoubleKo rubberStopperDoubleKo;//组合管
    Beaker beaker;//烧杯
    WaterStopClip waterStopClip;//夹子
    RubberStopper rubberStopper;//塞子
    IronPlatform IronPlatform;
    BeakerBrush beakerBrush;


    public static BigStepsManager Instance;

    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {
        roundBottpmFlask = m_RoundBottpmFlask.GetComponent<RoundBottpmFlask>();
        rubberStopperDoubleKo = m_RubberStopperDoubleKo.GetComponent<RubberStopperDoubleKo>();
        dropBottle = m_DropBottle.GetComponent<DropBottle>();
        rubberDropper = m_RubberDropper.GetComponent<RubberDropper>();
        beaker = m_Beaker.GetComponent<Beaker>();
        waterStopClip = m_WaterStopClip.GetComponent<WaterStopClip>();
        rubberStopper = m_RubberStopper.GetComponent<RubberStopper>();
        IronPlatform = m_IronPlatform.GetComponent<IronPlatform>();
        beakerBrush = m_BeakerBrush.GetComponent<BeakerBrush>();
    }

    public static bool IsStart;
    public void StartMove()
    {
        IsStart = true;
    }

    public void StopMove()
    {
        Invoke(nameof(StopIt), 2f);
    }

    public void StopIt()
    {
        IsStart = false;
    }

    public void SetBigStepOne()
    {
        m_StepsManager.ResetScore(1);
        m_StepsManager.ResetText(1);
        m_StepsManager.ResetData(1);


        roundBottpmFlask.SetBigStepTwo();
        rubberStopperDoubleKo.SetBigStepTwo();
        dropBottle.SetBigStepTwo();
        rubberDropper.SetBigStepTwo();
        beaker.SetBigStepTwo();
        waterStopClip.SetBigStepTwo();
        rubberStopper.SetBigStepTwo();

        roundBottpmFlask.isChecked = false;
        rubberStopperDoubleKo.isChecked = false;
        dropBottle.isChecked = false;
        rubberDropper.isChecked = false;
        beaker.isChecked = false;
        waterStopClip.isChecked = false;
        rubberStopper.isChecked = false;
        IronPlatform.isChecked = false;
    }

    public void SetBigStepTwo()
    {
        if (!StepsManager.Instance.BigStepButtons[0].enabled) return;
        StartMove();
        m_StepsManager.ResetScore(2);
        m_StepsManager.ResetText(2);
        m_StepsManager.ResetData(2);


        roundBottpmFlask.SetBigStepTwo();
        rubberStopperDoubleKo.SetBigStepTwo();
        dropBottle.SetBigStepTwo();
        rubberDropper.SetBigStepTwo();
        beaker.SetBigStepTwo();
        waterStopClip.SetBigStepTwo();
        rubberStopper.SetBigStepTwo();

        m_RoundBottpmFlask.transform.DOLocalMoveY(0.3f, 1f).OnComplete(() =>
        {
            roundBottpmFlask.SetStartMove();
            roundBottpmFlask.Round(180);
            m_RoundBottpmFlask.transform.DOLocalMoveX(-0.12f, 1f).OnComplete(() =>
            {
                roundBottpmFlask.SetOnPlatfrom();
                rubberStopper.ResetParent();
                m_RubberStopper.transform.DOLocalMoveY(0.17f, 1f).OnComplete(() =>
                {
                    m_RubberStopper.transform.DOLocalMoveX(-0.45f, 1f).OnComplete(() =>
                    {
                        rubberStopper.OnGround();
                        beaker.SetStartMove();
                        m_Beaker.transform.DOLocalMoveY(0.09f, 1f).OnComplete(() =>
                        {
                            m_Beaker.transform.DOLocalMoveX(-0.12f, 1f).OnComplete(() =>
                            {
                                beaker.SetStopMove();
                                m_RubberStopperDoubleKo.transform.DOLocalMoveY(0.22f, 1f).OnComplete(() =>
                                {
                                    rubberStopperDoubleKo.SetStartMove(true);
                                    m_RubberStopperDoubleKo.transform.DOLocalMoveX(-0.08f, 1f).OnComplete(() =>
                                    {
                                        rubberStopperDoubleKo.SetStopMove();
                                        waterStopClip.Close();
                                        a(dropNum);
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    }

    int dropNum;
    void a(int num)
    {
        rubberDropper.GetFtWater();
        rubberDropper.SetStartMove();
        m_RubberDropper.transform.DOLocalMoveY(0.18f, 1f).OnComplete(() =>
          {
              m_RubberDropper.transform.DOLocalMoveX(-0.13f, 1f).OnComplete(() =>
              {
                  rubberDropper.Drop();
                  m_RubberDropper.transform.DOLocalMoveX(-0.336f, 1f).OnComplete(() =>
                  {
                      rubberDropper.OnBottle();
                      StopMove();
                      if (dropNum > 1)
                      {
                          dropNum = 0;
                          return;
                      }
                      //a(dropNum++);
                  });
              });

          });
    }

    public void SetBigStepThree()
    {
        if (!StepsManager.Instance.BigStepButtons[1].enabled) return;
        StartMove();
        m_StepsManager.ResetScore(3);
        m_StepsManager.ResetText(3);
        m_StepsManager.ResetData(3);


        beaker.SetBigStepThree();
        roundBottpmFlask.SetBigStepThree();
        rubberStopperDoubleKo.SetBigStepThree();
        dropBottle.SetBigStepThree();
        rubberDropper.SetBigStepThree();
        waterStopClip.SetBigStepThree();
        rubberStopper.SetBigStepThree();
        waterStopClip.SetStartMove();
        waterStopClip.SetTarget(2);
        waterStopClip.SetStopMove();
        roundBottpmFlask.StartAction();
        Invoke(nameof(StopMove), 12f);
    }

    public void SetBigStepFour()
    {
        if (!StepsManager.Instance.BigStepButtons[2].enabled) return;
        StartMove();
        m_StepsManager.ResetScore(4);
        m_StepsManager.ResetText(4);
        m_StepsManager.ResetData(4);


        roundBottpmFlask.SetBigStepFour();
        rubberStopperDoubleKo.SetBigStepFour();
        dropBottle.SetBigStepFour();
        rubberDropper.SetBigStepFour();
        beaker.SetBigStepFour();
        waterStopClip.SetBigStepFour();
        rubberStopper.SetBigStepFour();

        waterStopClip.SetTarget(2);
        waterStopClip.Close();
        beaker.SetStartMove();
        m_Beaker.transform.DOLocalMoveX(0.04f, 1f).OnComplete(() =>
        {
            beaker.SetStopMove();
            StepsManager.Instance.StepFourNum[0] = true;
            StepsManager.Instance.SetStepFour(0);

            roundBottpmFlask.SetStartMove();
            roundBottpmFlask.ResetParent();
            m_RoundBottpmFlask.transform.DOLocalMoveY(0.37f, 1).OnComplete(() =>
            {
                m_RoundBottpmFlask.transform.DOLocalMoveX(-0.5f, 1).OnComplete(() =>
                {
                    roundBottpmFlask.Round(0);
                    m_RoundBottpmFlask.transform.DOLocalMoveY(0.1f, 1).OnComplete(() =>
                    {
                        roundBottpmFlask.SetOnGround();
                        rubberStopperDoubleKo.SetStartMove();
                        rubberStopperDoubleKo.ResetParent();
                        m_RubberStopperDoubleKo.transform.DOLocalMoveY(0.22f, 1f).OnComplete(() =>
                        {
                            m_RubberStopperDoubleKo.transform.DOLocalMoveX(-0.58f, 1f).OnComplete(() =>
                            {
                                rubberStopperDoubleKo.SetOnGround();
                                StepsManager.Instance.StepFourNum[1] = true;
                                StepsManager.Instance.SetStepFour(1);
                                StopMove();
                            });
                        });
                    });
                });
            });
        });
    }

    public void SetBigStepFive()
    {
        if (!StepsManager.Instance.BigStepButtons[3].enabled) return;
        StartMove();
        m_StepsManager.ResetScore(5);
        m_StepsManager.ResetText(5);
        m_StepsManager.ResetData(5);


        roundBottpmFlask.SetBigStepFive();
        rubberStopperDoubleKo.SetBigStepFive();
        dropBottle.SetBigStepFive();
        rubberDropper.SetBigStepFive();
        beaker.SetBigStepFive();
        waterStopClip.SetBigStepFive();
        rubberStopper.SetBigStepFive();

        roundBottpmFlask.SetStartMove();
        roundBottpmFlask.transform.DOLocalMoveY(0.27f, 1).OnComplete(() =>
        {
            roundBottpmFlask.transform.DOLocalMoveX(-0.86f, 1).OnComplete(() =>
            {
                roundBottpmFlask.transform.rotation = Quaternion.Euler(0, 0, 120);
                roundBottpmFlask.m_FlaskWater.SetActive(false);
                Invoke("SetBigStepFive2", 1f);
            });
        });
    }

    public void SetBigStepFive2()
    {
        roundBottpmFlask.transform.rotation = Quaternion.Euler(0, 0, 0);
        roundBottpmFlask.transform.DOLocalMoveX(-0.423f, 1).OnComplete(() =>
        {
            roundBottpmFlask.transform.DOLocalMoveY(0.05f, 1).OnComplete(() =>
            {
                roundBottpmFlask.SetStopMove();
                StepsManager.Instance.StepFiveNum[0] = true;
                StepsManager.Instance.SetStepFive(0);
                beaker.SetStartMove();
                beaker.transform.DOLocalMoveY(0.27f, 1).OnComplete(() =>
                {
                    beaker.transform.DOLocalMoveX(-0.86f, 1).OnComplete(() =>
                    {
                        beaker.transform.rotation = Quaternion.Euler(0, 0, 120);
                        beaker.m_Water.SetActive(false);
                        Invoke("SetBigStepFive3", 1f);

                    });
                });
            });
        });
    }

    public void SetBigStepFive3()
    {
        beaker.transform.rotation = Quaternion.Euler(0, 0, -90);
        beaker.transform.DOLocalMoveX(0.04f, 1).OnComplete(() =>
        {
            beaker.transform.DOLocalMoveY(0.3f, 1).OnComplete(() =>
            {
                beakerBrush.SetStartMove();
                beakerBrush.transform.DOLocalMoveY(0.35f, 1).OnComplete(() =>
                 {
                     beakerBrush.transform.rotation = Quaternion.Euler(0, 0, -90);
                     beakerBrush.transform.DOLocalMoveX(0.3f, 1).OnComplete(() =>
                      {
                          beakerBrush.transform.DOLocalMoveY(0.3f, 1).OnComplete(() =>
                          {
                              beakerBrush.transform.DOLocalMoveX(0.14f, 1).OnComplete(() =>
                              {
                                  beakerBrush.transform.DOLocalMoveX(0.3f, 1).OnComplete(() =>
                                  {
                                      beakerBrush.transform.DOLocalMoveX(0.14f, 1).OnComplete(() =>
                                      {
                                          beakerBrush.transform.DOLocalMoveX(0.3f, 1).OnComplete(() =>
                                          {
                                              beakerBrush.transform.DOLocalMoveY(0.35f, 1).OnComplete(() =>
                                                {
                                                    beakerBrush.transform.rotation = Quaternion.Euler(0, 0, 0);
                                                    beakerBrush.transform.DOLocalMoveX(-0.716f, 1).OnComplete(() =>
                                                    {
                                                        beakerBrush.SetStopMove();
                                                        beaker.transform.rotation = Quaternion.Euler(0, 0, 0);
                                                        beaker.transform.DOLocalMoveY(0.27f, 1).OnComplete(() =>
                                                        {
                                                            StopMove();
                                                            beaker.SetStopMove();
                                                            m_RubberStopperDoubleKo.GetComponent<RubberStopperDoubleKo>().ResetPosition();
                                                            StepsManager.Instance.StepFiveNum[1] = true;
                                                            StepsManager.Instance.SetStepFive(1);
                                                            StepsManager.Instance.StepFiveNum[2] = true;
                                                            StepsManager.Instance.SetStepFive(2);
                                                        });
                                                    });
                                                });
                                          });
                                      });
                                  });
                              });
                          });
                      });
                 });
            });

        });
    }
}
